package com.guiji.text2video.job.task;

import cn.hutool.core.lang.Validator;
import com.guiji.text2video.job.domain.Text2videoJob;
import com.guiji.text2video.job.mapper.Text2videoJobMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Copyright (C), 2018-2021
 * @FileName: PushOnlineNews
 * @Author: scl
 * @Date: 2021/11/12 14:04
 * @Description:
 */

@Component("on")
public class PushOnlineNews {


    private static final Logger logger = LoggerFactory.getLogger(PushOnlineNews.class);

    @Autowired(required = false)
    Text2videoJobMapper jobMapper;

    @Autowired
    PushTask pushTask;

    /**
     * @descp: 1, 查询类别 2，channel 3,cookie
     * @param: 4，推送大小
     * @Author: scl
     * @Date: 2021/11/12 14:08
     * <p>
     * on.p('体育','最新体育新闻','dongmanerciyuanzixun',5)
     * on.p('社会','社会热点','mingshengzixunbang',5)
     * on.p('军事','最新军情资讯','junqingzixun',5)
     * on.p('财经','最新财经资讯','aicaijing',5)
     * <p>
     * on.p('凤凰网科技','最新科技资讯','aikeji',5)
     * on.p('新浪科技=网易科技','最新科技新闻','zongyiyingshizixun',5)
     * on.p('IndiaTech','Latest Tech News','englishTech',5)
     * on.p('知乎','知道多一点','zhihuribao',5)
     * <p>
     * on.p('XinhuaWorld','latest world news','viewworldnews',5)
     *
     *  on.p('XinhuaBusiness-EnglishBusiness=EnPeople-EnglishBusiness=ChinaDaily-EnglishBusiness=ChinaOrg-EnglishBusiness','Business trend of China','yuer',5)
     *  on.p('Techcrunch-IndiaTech','View Technology','zhihuribao',5)
     *  on.p('Techcrunch-IndiaTech=People-EnglishWorld=ChinaOrg-EnglishWorld=ChinaDaily-EnglishWorld','focus headlines','meishi',4)
     *  on.p('Timesofindia-EnglishBusiness=Marketwatch-EnglishBusiness','Business World Today','shuiqianjieyoushi',5)
     *  on.p('Coindesk-Cryptocurrency=Beincrypto-Cryptocurrency','Latest Crypto News','kepu',4)
     *  on.p('Fxempire-Cryptocurrency','Cryptocurrency News','jiamihuobi',5)
     *  on.p('Dailyfx-Forex=Investing-Forex','FXInformation','infofx',3)
     *  on.p('Rttnews-Forex=Fxnewsgroup-Forex','fxrelatednews','fxrelated',3)
     *
     *
     * on.p('体育','最新体育新闻','dongmanerciyuanzixun',5)
     * on.p('体育','最新体育新闻','dongmanerciyuanzixun',5)
     * on.p('体育','最新体育新闻','dongmanerciyuanzixun',5)
     * on.p('体育','最新体育新闻','dongmanerciyuanzixun',5)
     * on.p('体育','最新体育新闻','dongmanerciyuanzixun',5)
     * on.p('体育','最新体育新闻','dongmanerciyuanzixun',5)
     * on.p('体育','最新体育新闻','dongmanerciyuanzixun',5)
     */
    public void p(String category, String channel, String cookie, Integer size) {

        //支持多种类别,查找
        List<Text2videoJob> listJobs=new ArrayList<>();
        String[] cates = category.split("=");
        for (String c : cates) {
            listJobs.addAll(getList(c));
            if(listJobs.size() > size){
                break;
            }
        }
        if (listJobs.size() > size) {
            listJobs = listJobs.subList(0, size);
        }
        pushTask.getTop5(listJobs, channel, cookie, ":5556/api/upload");
    }


    private  List<Text2videoJob> getList(String category) {
        Text2videoJob job = new Text2videoJob();
        if (Validator.hasChinese(category)) {
            job.setTtsType("-1");
        } else {
            job.setTtsType("-3");
        }
        job.setIsSuccess("1");
        job.setBackupText(category);
        List<Text2videoJob> listJobs = jobMapper.selectText2videoJobListByEndTime(job);
        logger.info("每60分钟尝试推送一次视频,24小时内集合《{}》大小：{}", category, listJobs.size());
        return listJobs;
    }
}